﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class product_type_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 返回判断
                string backType = ChangeHope.WebPage.PageRequest.GetString("backType");
                if (backType == "Set")
                {
                    this.towProductType.Visible = true;
                    this.oneProductType.Visible = false;
                }
                #endregion
                int id = ChangeHope.WebPage.PageRequest.GetInt("id");
                if (id > 0)
                {
                    BindInfo(id);
                }
                if (ViewState["ID"] == null)
                {
                    YXShop.Common.PromptInfo.Popedom("001003002", "对不起，您没有新增商品类型的权限");
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("001003004", "对不起，您没有编辑商品类型的权限");
                }
            }
        }
        #region 绑定信息
        protected void BindInfo(int id)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductType model = bll.GetModel(id);
            if (model != null)
            {
                this.txtTypeName.Text = model.TypeName;
                this.txtAliasName.Text = model.TypeAliasName;
                this.rblEntityProduct.SelectedValue = model.IsEntityProduct.ToString();
                this.rblProductPropery.SelectedValue = model.ProperyTableName == "" ? "0" : "1";
                this.rblProperyParameter.SelectedValue = model.ParameterTableName == "" ? "0" : "1";
                this.rblRelevanceBrand.SelectedValue = model.RelevanceBrand == "" ? "0" : "1";
                this.brlBuySet.SelectedValue = model.BuyOptionSetTableName == "" ? "0" : "1";
                ViewState["ID"] = model.Id;
                ViewState["ParameterTableName"] =model.ParameterTableName;
                ViewState["ProperyTableName"] =model.ProperyTableName ;
                ViewState["RelevanceBrand"] =model.RelevanceBrand;
                ViewState["BuyOptionSetTableName"] =model.BuyOptionSetTableName ;
            }
        }
        #endregion

        #region 第一步
        protected void button1_Click(object sender, EventArgs e)
        {
            if (this.txtTypeName.Text.Trim() == "" || this.txtTypeName.Text.Trim() == string.Empty)
            {
                this.ltlMsg.Text = "操作失败，请输入类型名称！";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            YXShop.Model.Product.ProductType model = new YXShop.Model.Product.ProductType();
            model.TypeName = this.txtTypeName.Text.Trim();
            model.TypeAliasName = this.txtAliasName.Text.Trim();
            model.IsEntityProduct =Convert.ToInt32(this.rblEntityProduct.SelectedValue);
            model.ParameterTableName = ViewState["ParameterTableName"] != null ? ViewState["ParameterTableName"] .ToString(): string.Empty;
            model.ProperyTableName = ViewState["ProperyTableName"] != null ? ViewState["ProperyTableName"].ToString() : string.Empty;
            model.RelevanceBrand = ViewState["RelevanceBrand"] != null ? ViewState["RelevanceBrand"].ToString() : string.Empty;
            model.BuyOptionSetTableName = ViewState["BuyOptionSetTableName"] != null ? ViewState["BuyOptionSetTableName"].ToString() : string.Empty;
            model.UpdateTime = System.DateTime.Now;
            if (ViewState["ID"] == null)
            {
                int id=bll.Add(model);
                if (id > 0)
                {
                    ViewState["ID"] = id;
                    this.towProductType.Visible = true;
                    this.oneProductType.Visible = false;
                }
            }
            else
            {
                model.Id = Convert.ToInt32(ViewState["ID"]);
                if (bll.Update(model) > 0)
                {
                    this.towProductType.Visible = true;
                    this.oneProductType.Visible = false;
                }
            }
        }
        #endregion

        protected void button2_Click(object sender, EventArgs e)
        {
            this.towProductType.Visible = false;
            this.oneProductType.Visible =true ;
        }

        protected void button4_Click(object sender, EventArgs e)
        {
            string classValue = string.Empty;
            if (this.rblProductPropery.SelectedValue == "1")
            {
                classValue += "1,";
            }
            if (this.rblProperyParameter.SelectedValue == "1")
            {
                classValue += "2,";
            }
            if (this.rblRelevanceBrand.SelectedValue == "1")
            {
                classValue += "3,";
            }
            if (this.brlBuySet.SelectedValue == "1")
            {
                classValue += "4,";
            }
            if (this.rblProductPropery.SelectedValue == "1")
            {
                Response.Redirect("productproperty_list.aspx?classValue=" + classValue + "&id=" + ViewState["ID"] + "");
            }
            else if (this.rblProperyParameter.SelectedValue == "1")
            {
                Response.Redirect("productparameter_list.aspx?classValue=" + classValue + "&id=" + ViewState["ID"] + "");
            }
            else if (this.rblRelevanceBrand.SelectedValue == "1")
            {
                Response.Redirect("productother_list.aspx?classValue=" + classValue + "&id=" + ViewState["ID"] + "");
            }
            else if (this.brlBuySet.SelectedValue == "1")
            {
                Response.Redirect("productbuyset_list.aspx?classValue=" + classValue + "&id=" + ViewState["ID"] + "");
            }
        }
    }
}
